package libraries;


public class TransPart {

	private String device;
	private String pack;
	private String library;
	private String comment;
	private TransConn conn;
	
	private String moPath;
	
	private int valueType;
	// 0= no  value, use EAGLE-value only
	// 1= use value as prefix (value "V=", EAGLE hands over "200", result "V=200")
	// 2= use value entirely, discard EAGLE-value (useful if EAGLE value is too irregular)
	String value;
	public String getDevice() {
		return device;
	}
	public void setDevice(String device) {
		this.device = device;
	}
	public String getPack() {
		return pack;
	}
	public void setPack(String pack) {
		this.pack = pack;
	}
	public String getLibrary() {
		return library;
	}
	public void setLibrary(String library) {
		this.library = library;
	}
	public String getComment() {
		return comment;
	}
	public void setComment(String comment) {
		this.comment = comment;
	}
	public String getMoPath() {
		return moPath;
	}
	public void setMoPath(String moPath) {
		this.moPath = moPath;
	}
	public int getValuetype() {
		return valueType;
	}
	public void setValuetype(int valuetype) {
		this.valueType = valuetype;
	}
	public String getValue() {
		return value;
	}
	public void setValue(String value) {
		this.value = value;
	}
	public TransConn getConn() {
		return conn;
	}
	public void setConn(TransConn conn) {
		this.conn = conn;
	}
	public TransPart(String device, String pack, String library,
			String comment, TransConn conn, String moPath, int valuetype,
			String value) {
		super();
		this.device = device;
		this.pack = pack;
		this.library = library;
		this.comment = comment;
		this.conn = conn;
		this.moPath = moPath;
		this.valueType = valuetype;
		this.value = value;
	}
	
	/**
	 * checks whether a part's device, library and package entries match those of another part
	 * @param oPart the part to be checked against
	 * @return
	 */
	private boolean originMatches (TransPart oPart) {
		if(this.getDevice().equals(oPart.getDevice()) && this.getLibrary().equals(oPart.getLibrary()) && this.getPack().equals(oPart.getPack())) {
			return true;
		} else {
			return false;
		}
	}
	
	/**
	 * checks whether two TransParts describe the same part
	 * @param oPart
	 * @return
	 */
	public boolean matches(TransPart oPart) {
		if(originMatches(oPart)) {
			if(this.getMoPath().equals(oPart.getMoPath()) && this.getComment().equals(oPart.getComment()) && this.getValue().equals(oPart.getValue()) && (this.getValuetype() == oPart.getValuetype())) {
				return true;
			} else {
				return false;
			}
		} else {
			return false;			
		}
	}
	
	/**
	 * checks whether two TransParts describe the same EAGLE part, but return a different modelica representation
	 * @param oPart
	 * @return
	 */
	public boolean badMatches(TransPart oPart){
		if(originMatches(oPart)) {
			if(this.getMoPath().equals(oPart.getMoPath()) && this.getComment().equals(oPart.getComment()) && this.getValue().equals(oPart.getValue()) && (this.getValuetype() == oPart.getValuetype())) {
				return false;
			} else {
				return true;
			}
		} else {
			return false;			
		}
	}
	
	/**
	 * 
	 * @return a string representing all traits of a TransPart
	 */
	public String toLongString() {
		String result = "";
		result += "Device: " + device;
		result += "\nPackage: " + pack;
		result += "\nLibrary: " + library;
		result += "\nComment: " + comment;
		result += "\n" + conn.toString();
		result += "\nModelica Model: " + moPath;
		result += "\nValue Type: " + vtToString(valueType);
		result += "\nValue: " + value;
						
		return result;
	}
	
	/**
	 * 
	 * @return a string containing only the device and modelica path traits of a TransPart
	 */
	public String toShortString() {
		String result = "";
		result += "Device: " + device;

		result += " MODELICA Path: " + moPath;
		return result;
}
	
	public String toString() {
		return toShortString();
	}
	
	/**
	 * 
	 * @return a representation of a TransPart to be exported to an .edl file
	 */
	public String toFileString() {
		String connString = conn.toFileString();
		return (device + "|" + pack + "|" + library + "|" + comment + "|" + moPath + "|" + valueType + "|" + value + System.getProperty("line.separator") + connString);
	}
	
	public static String vtToString (int vt) {
		switch(vt){
			case 0:		return "Ignore";
			case 1:		return "Use as prefix";
			default:	return "Use instead of EAGLE value";
		}
		
	}

}
